import axios from 'axios';

let base = '/api';
var qs = require('qs');

export const requestLogin = params => { return axios.post(`${base}/admin/login`, params).then(res => res.data); };

export const getUserList = params => { return axios.get(`${base}/admin/user/list`, { params: params }); };

export const getUserListPage = params => { return axios.get(`${base}/admin/user/listpage`, { params: params }); };

export const removeUser = params => { return axios.get(`${base}/admin/user/remove`, { params: params }); };

export const batchRemoveUser = params => { return axios.get(`${base}/admin/user/batchremove`, { params: params }); };

export const editUser = params => { return axios.get(`${base}/admin/user/edit`, { params: params }); };

export const addUser = params => { return axios.get(`${base}/admin/user/add`, { params: params }); };

export const test = params => { return axios.get(`/api/admin/user/getUser`, { params: params }); };

//登录
export const login = (params, router) => { return post(`/api/admin/user/login`, params, router) };

//系统配置模块
//数据库表模块
export const getSysTableList = (params, router) => {return getReq(`/api/admin-table/table`, params, router)};
export const addSysTable =     (params, router) => {return postReq(`/api/admin-table/table`, params, router)};
export const deleteSysTable =  (params, router) => {return deleteReq(`/api/admin-table/table/`, params, router)};
export const updateSysTable =  (params, router) => {return putReq(`/api/admin-table/table`, params, router)};
export const deleteMuti = 	   (params, router) => {return deleteReq(`/api/admin-table/table/muti/`, params, router)};

//菜单模块
export const getSysMenuList = 			 (params, router) => {return getReq(`/api/admin-menu/menu/`, params, router)};
export const getSysMenuListByCondition = (params, router) => {return getReq(`/api/admin-menu/menu/listByCondition`, params, router)};
export const getSysMenuTree = 			 (params, router) => {return getReq(`/api/admin-menu/menu/treeData`, params, router)};
export const addSysMenu = 				 (params, router) => {return postReq(`/api/admin-menu/menu`, params, router)};
export const updateSysMenu = 			 (params, router) => {return putReq(`/api/admin-menu/menu`, params, router)};
export const deleteSysMenu = 			 (params, router) => {return deleteReq(`/api/admin-menu/menu/`, params, router)};
export const getOneByKey = 				 (params, router) => {return getReq(`/api/admin-menu/menu/`+params, null, router)};



//转换headers中的Content-Type类型，json转为x-www-form-urlencoded，以便后端接收
function getReq(url, params, router){
	return axios.get(url, { params: params }).then(res => { 
			handleCallback(res);
			return res.data;
		});
}

function postReq(url, params, router){
	return axios.post(url, 
		qs.stringify(params),
		{
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			}
		}).then(res => { 
			handleCallback(res);
			return res.data;
		});
}

function putReq(url, params, router){
	return axios.put(url, 
		qs.stringify(params),
		{
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			}
		}).then(res => { 
			handleCallback(res);
			return res.data;
		});
}

function deleteReq(url, params, router){
	return axios.delete(url, { params: params }).then(res => { 
			handleCallback(res);
			return res.data;
		});
}

function handleCallback(res){
	if(!res.data.success && res.data.errMsg == '请先登录！'){
		router.push({ path: '/login' });
	}
}

